package com.laiketui.common.service.dubbo.banner;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laiketui.common.api.banner.BannerService;
import com.laiketui.common.mapper.BannerMapper;
import com.laiketui.common.service.dubbo.BaseServiceImpl;
import com.laiketui.common.utils.tool.cache.RedisDataTool;
import com.laiketui.core.cache.RedisUtil;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.domain.banner.Banner;
import com.laiketui.domain.index.IndexData;
import com.laiketui.domain.lktconst.DictionaryConst;
import com.laiketui.domain.mch.AdminModel;
import com.laiketui.domain.vo.MainVo;
import com.laiketui.domain.vo.banner.BannerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
* @author 34496
* @description 针对表【lk_banner】的数据库操作Service实现
* @createDate 2025-01-08 23:40:21
*/
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public int insertBanner(BannerVo vo) {
        AdminModel user = RedisDataTool.getRedisAdminUserCache(vo.getAccessId(), redisUtil);
        Banner banner = new Banner();
        if(vo.getType() == null){
            banner.setType(GloabConst.BannerType.BANNER_TYPE_INDEX);
        }else{
            banner.setType(vo.getType());
        }
        banner.setBannerPath(vo.getBannerPath());
        banner.setProductId(vo.getProductId());
        banner.setStatus(vo.getStatus());
//        banner.setStatus(DictionaryConst.StatusType.STATUS_ISLINE);
        banner.setSort(vo.getSort());
        banner.setStoreId(vo.getStoreId());
        return bannerMapper.insert(banner);
    }

    @Override
    public int updateBanner(BannerVo vo) {
        AdminModel user = RedisDataTool.getRedisAdminUserCache(vo.getAccessId(), redisUtil);
        Banner banner = new Banner();
        banner.setId(vo.getId());
        banner = bannerMapper.selectOne(banner);
        banner.setBannerPath(vo.getBannerPath());
        banner.setProductId(vo.getProductId());
        banner.setStatus(vo.getStatus());
        banner.setSort(vo.getSort());
        banner.setStoreId(vo.getStoreId());
        return bannerMapper.updateByPrimaryKey(banner);
    }

    @Override
    public Banner selectInfo(BannerVo vo) {
        AdminModel user = RedisDataTool.getRedisAdminUserCache(vo.getAccessId(), redisUtil);
        Banner banner = new Banner();
        banner.setId(vo.getId());
        return bannerMapper.selectOne(banner);
    }

    @Override
    public PageInfo<Banner> selectList(MainVo vo) {
        AdminModel user = RedisDataTool.getRedisAdminUserCache(vo.getAccessId(), redisUtil);
        Banner banner = new Banner();
        banner.setStoreId(vo.getStoreId());
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<Banner> list = bannerMapper.select(banner);
        PageInfo<Banner> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int deleteBanner(BannerVo vo) {
        Banner banner = new Banner();
        banner.setId(vo.getId());
        return bannerMapper.delete(banner);
    }

    @Override
    public int updateStatus(BannerVo vo) {
        AdminModel user = RedisDataTool.getRedisAdminUserCache(vo.getAccessId(), redisUtil);
        Banner banner = new Banner();
        banner.setId(vo.getId());
        banner = bannerMapper.selectOne(banner);
        Integer status = banner.getStatus();
        if(DictionaryConst.StatusType.STATUS_ISLINE.equals(status)){
            banner.setStatus(DictionaryConst.StatusType.STATUS_OFFLINE);
        }
        if(DictionaryConst.StatusType.STATUS_OFFLINE.equals(status)){
            banner.setStatus(DictionaryConst.StatusType.STATUS_ISLINE);
        }
        return bannerMapper.updateByPrimaryKeySelective(banner);
    }

    @Override
    public List<Banner> bannerList(BannerVo vo) {
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort asc");
        criteria.andEqualTo("status", DictionaryConst.StatusType.STATUS_ISLINE);
        criteria.andEqualTo("storeId",vo.getStoreId());
        if(vo.getType() == null){
            criteria.andEqualTo("type",GloabConst.BannerType.BANNER_TYPE_INDEX);
        }else{
            criteria.andEqualTo("type",vo.getType());
        }
        List<Banner> banners = bannerMapper.selectByExample(example);
        return banners;
    }
}




